package fr.esiag.isidis.epp.service;


import java.io.FileInputStream;
import java.io.FileOutputStream;

import javax.xml.bind.JAXBElement;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

import org.springframework.oxm.Marshaller;
import org.springframework.oxm.Unmarshaller;

import fr.esiag.isidis.epp.generatedClasses.PointConso;
import fr.esiag.isidis.epp.generatedClasses.QueryExportCdc;

public class JaxbConverter {

	private Marshaller marshaller;

	private Unmarshaller unmarshaller;
	
	 public void marshallerPointConso(String fileName, PointConso pointConso)
	{
		FileOutputStream os;
		try {
			os = new FileOutputStream(fileName);
			this.marshaller.marshal(pointConso, new StreamResult(os));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public QueryExportCdc unmarshalerPointConso(String fileName)
	{
		try {
			FileInputStream is = new FileInputStream(fileName);
			JAXBElement<QueryExportCdc> element = (JAXBElement<QueryExportCdc> )unmarshaller.unmarshal(new StreamSource(is));
			QueryExportCdc retour = element.getValue();
			return retour;
		} catch (Exception e) {
			return null;
		}
	} 
	
	public Marshaller getMarshaller() {
		return marshaller;
	}

	public void setMarshaller(Marshaller marshaller) {
		this.marshaller = marshaller;
	}

	public Unmarshaller getUnmarshaller() {
		return unmarshaller;
	}

	public void setUnmarshaller(Unmarshaller unmarshaller) {
		this.unmarshaller = unmarshaller;
	}
	
}
